package com.self.study.subjectsstudy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.self.study.subjectsstudy.common.ResultInfo;
import com.self.study.subjectsstudy.common.SubjectsItemVo;
import com.self.study.subjectsstudy.db.bean.ErrorSubjects;
import com.self.study.subjectsstudy.db.bean.Subjects;
import com.self.study.subjectsstudy.db.mapper.ErrorSubjectsMapper;
import com.self.study.subjectsstudy.db.mapper.SubjectsMapper;
import com.self.study.subjectsstudy.service.ISubjectsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lixunhuan
 * @since 2021-03-06
 */
@Service
public class SubjectsServiceImpl extends ServiceImpl<SubjectsMapper, Subjects> implements ISubjectsService {

    @Autowired
    private SubjectsMapper subjectsMapper;
    @Autowired
    private ErrorSubjectsMapper errorSubjectsMapper;

    @Override
    public List<SubjectsItemVo> getItems(Integer subjectType, Integer level) {
        List<SubjectsItemVo> items = new ArrayList<>();
        if (level == 3) {//专项训练
            subjectsMapper.getItems(subjectType, level).stream().forEach(o -> {
                SubjectsItemVo subjectsItemVo = new SubjectsItemVo();
                subjectsItemVo.setItemId(o.intValue());
                subjectsItemVo.setItem("第" + o.intValue() + "章");
                items.add(subjectsItemVo);
            });
        } else {
            subjectsMapper.getItems(subjectType, level).stream().forEach(o -> {
                SubjectsItemVo subjectsItemVo = new SubjectsItemVo();
                subjectsItemVo.setItemId(o.intValue());
                subjectsItemVo.setItem("计算机等级考试试卷第" + o.intValue() + "套");
                items.add(subjectsItemVo);
            });
        }

        return items;
    }

    @Override
    public ResultInfo answerSubject(String openid, String option, Integer subjectId) {
        String subjectoption = subjectsMapper.selectById(subjectId).getAnswer().trim();
        if (errorSubjectsMapper.selectCount(new QueryWrapper<ErrorSubjects>().eq("openid", openid).eq("subject_id", subjectId)) > 0) {
            return new ResultInfo(ResultInfo.Status.SUCCESS).message("该题已经答错过一次");
        }
        if (!Objects.equals(option, subjectoption)) {
            ErrorSubjects errorSubjects = new ErrorSubjects();
            errorSubjects.setOpenid(openid);
            errorSubjects.setSubjectId(subjectId);
            errorSubjectsMapper.insert(errorSubjects);
            return new ResultInfo(ResultInfo.Status.SUCCESS).message("插入我的错题成功");
        }
        return new ResultInfo(ResultInfo.Status.SUCCESS).message("答题正确");
    }

    @Override
    public ResultInfo getMyErrorSubjects(String openid) {

        return new ResultInfo(ResultInfo.Status.SUCCESS).result(errorSubjectsMapper.getMyErrorSubjects(openid));
    }

    @Override
    public ResultInfo getSubjectsByLevelAndType(Integer subjectType, Integer level) {

        List<Subjects> subjectsList;

        if (subjectType == 1) {
            subjectsList = subjectsMapper.getBySubjectType1(level);
        } else {
            subjectsList = subjectsMapper.getBySubjectType2(level);
        }

        return new ResultInfo(ResultInfo.Status.SUCCESS).result(subjectsList);
    }
}
